package com.beimin.evedata.hibernate;

import com.beimin.evedata.model.inv.InvTypeMaterial;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.beimin.evedata.model.ram.RamActivity;
import com.beimin.evedata.model.ram.RamTypeRequirement;
import com.beimin.evedata.model.ram.TypeActivityMaterial;
import com.beimin.evedata.repository.RamRepository;

public class HibRamRepository implements RamRepository{
	private final SessionFactory sf;

	public HibRamRepository(SessionFactory sf) {
		this.sf = sf;
	}
	@SuppressWarnings("unchecked")
  @Override
	public Map<Integer, RamActivity> getActivities() {
		Map<Integer, RamActivity> result = new HashMap<Integer, RamActivity>();
		List<RamActivity> list = sf.getCurrentSession().createCriteria(RamActivity.class).list();
		for (RamActivity ramActivity : list) {
			result.put(ramActivity.getActivityID(), ramActivity);
		}
		return result;
	}

  @Override
	public RamActivity getActivity(int activityID) {
		return (RamActivity) sf.getCurrentSession().get(RamActivity.class, activityID);
	}

  @Deprecated
	@SuppressWarnings("unchecked")
  @Override
	public Collection<TypeActivityMaterial> getTypeActivityMaterials(int typeID, int activityID) {
		Session session = sf.getCurrentSession();
		Criteria criteria = session.createCriteria(TypeActivityMaterial.class);
		criteria.add(Restrictions.eq("id.typeID", typeID));
		criteria.add(Restrictions.eq("id.activityID", activityID));
		return criteria.list();
	}

  @Override
	public Collection<InvTypeMaterial> getTypeMaterialsRequired(int typeID) {
		Session session = sf.getCurrentSession();
		Criteria criteria = session.createCriteria(InvTypeMaterial.class);
		criteria.add(Restrictions.eq("id.type.typeID", typeID));
		return criteria.list();
	}
  
  @Override
  public Collection<RamTypeRequirement> getTypeRequirements(int typeID) {
		Session session = sf.getCurrentSession();
		Criteria criteria = session.createCriteria(RamTypeRequirement.class);
		criteria.add(Restrictions.eq("id.type.typeID", typeID));
		return criteria.list();
  }
}